package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author EllieHy
 */
@Component
@Slf4j
@RequiredArgsConstructor  //在编译器进行编译,构造函数生成
public class LessonChangeListener {
	private final ILearningLessonService learningLessonService;
	
	@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.lesson.pay.queue", durable = "true"),
			exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),
			key = MqConstants.Key.ORDER_PAY_KEY))
	public void listenLessonPay(OrderBasicDTO orderBasicDTO) {
		log.info("接收到消息,课程id是:{},用户id是:{}", orderBasicDTO.getCourseIds(), orderBasicDTO.getUserId());
		//判断消息数据是否有误
		if (orderBasicDTO.getUserId() == null
				|| orderBasicDTO.getOrderId() == null ||
				CollUtils.isEmpty(orderBasicDTO.getCourseIds())) {
			log.error("接收到mq消息有误,订单数据为空");
			return;
		}
		//添加课程
		log.debug("监听到用户{}的订单{}，需要添加课程{}到课表中", orderBasicDTO.getUserId(), orderBasicDTO.getOrderId(),
				orderBasicDTO.getCourseIds());
		learningLessonService.addUserLessons(orderBasicDTO.getUserId(), orderBasicDTO.getCourseIds());
	}
}
